package org.algorithm.熊猫.树;


import org.algorithm.common.Node;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author jzx
 * @date 2022/5/30
 * @desc {https://leetcode.cn/problems/n-ary-tree-level-order-traversal/}
 */
public class $6月4N叉树的层序遍历 {
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> res = new ArrayList<>();

        if (root == null) {
            return res;
        }
        Queue<Node> queue = new LinkedList<>();
        queue.offer(root);

        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Integer> levelList = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                Node poll = queue.poll();
                levelList.add(poll.val);
                List<Node> children = poll.children;
                if (children != null && !children.isEmpty()) {
                    children.forEach(queue::offer);
                }
            }
            res.add(levelList);
        }

        return res;

    }
}
